package com.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;







import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.wd_ht_build;
import com.bean.wd_ht_project;
import com.bean.wd_ht_unit;
import com.service.BuildService;
import com.service.ProjectService;
import com.service.UnitService;

@Controller
@RequestMapping("/unit")
public class UnitAction {
	@Autowired
	private UnitService unitservice;
	@Autowired
	private ProjectService projectservice;
	@Autowired
	private BuildService bulidservice;
	/**
	 * 查询所有的单元信息
	 * @param model 传参
	 * @return
	 */
	@RequestMapping(value="/selectall.do")
	public String selectAll(Model model){
		List<wd_ht_unit> list = unitservice.selectAllToBuildToProject();
		model.addAttribute("list",list);
		System.out.println("查询单元信息-------------------"+list);
		return "UnitManager";
	}
	/**
	 * 跳转到添加单元页面
	 * @param model 传参
	 * @return
	 */
	@RequestMapping(value="/add.do")
	public String add(Model model){
		List<wd_ht_project> plist = projectservice.selectAll();//查询所有的项目信息和楼栋信息，将项目名称和id传到前台
		model.addAttribute("plist",plist);
		return "UnitAddManager";
	}
	/**
	 * 添加单元信息
	 * @param ubean
	 * @return
	 */
	@RequestMapping(value="/insert.do")
	public String insert(wd_ht_unit ubean){
		System.out.println("添加单元----------------------------------"+ubean);
		int num = unitservice.insert(ubean);
		return "redirect:selectall.do";
	}
	/**
	 * 批量删除(可以删一个)
	 * @param buildIdArray 前台传来所有需要删除的单元id
	 * @return
	 */
	@RequestMapping(value="/deletebyprimarykey.do")
	public String deleteByPrimaryKey(String[] unitIdArray){
		for(String s:unitIdArray){
			System.out.println("楼栋id----------------------------------"+s);
			int num = unitservice.deleteByPrimaryKey(s);
		}
		return "redirect:selectall.do";
	}
	@RequestMapping(value="/selectone.do")
	public String selectone(String unitId,Model model){
		System.out.println("跳转添加页面单元id----------------------------------"+unitId);
		wd_ht_unit ubean = unitservice.selectByPrimaryKey(unitId);
		model.addAttribute("ubean",ubean);
		List<wd_ht_project> plist = projectservice.selectAll();//查询所有的项目信息，将项目名称和id传到前台
		model.addAttribute("plist",plist);
		return "UnitUpdateManager";
	}
	/**
	 * 修改单元信息
	 * @param pbean 修改之后的单元信息
	 * @return
	 */
	@RequestMapping(value="/update.do")
	public String update(wd_ht_unit ubean){
		ubean.setUpdateUser("修改人从session中拿！");//写session中修改人的信息
		Date day = new Date();
		SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			ubean.setUpdateTime(fm.parse(fm.format(day)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("修改后的楼栋信息----------------------------------"+ubean);
		int num = unitservice.updateByPrimaryKeySelective(ubean);
		return "redirect:selectall.do";
	}
	/**
	 * ajax添加表单联动
	 * @param request
	 * @param response
	 * @param projectId
	 */
	@RequestMapping(value="/selectBuild.do")
	public void selectBuild(HttpServletRequest request, HttpServletResponse response,String projectId){
		System.out.println("项目id--------------"+projectId);
		System.out.println("跳转到ajax了-----------------------");
		List<wd_ht_build> list = bulidservice.selectAllByProject(projectId);
		System.out.println("list-----------"+list);
		try {
			/*response.getWriter().print(list);*/
			String json = JSONArray.fromObject(list).toString();
			response.setHeader("Cache-Control", "no-cache");
			response.setContentType("text/json; charset=utf-8");
			response.getWriter().print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
